﻿using AdinZ.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading;
using System.Web.Configuration;

namespace Reader.Api
{
    public class Action
    {

        public static string action(Dictionary<string, string> d)
        {
            string imei, version = null, model = null, os = null, os_name = null, screen_size = null, type = null, cu = "", agent = "",
                page = "0", id = "1", session = "", payment = "0.0", uid = "0", chapterid = "0", bookid = "0", name = "", passwd = "",
                email = "", phone = "", vid = "", pid = "", firm="",kernel="",sgin="",ispay="";
            string JsonStr = "{\"success\": false, \"result\": {}}";
            string action = d["action"];
            try { imei = d["imei"]; }
            catch { imei = ""; }
            try { version = d["version"]; }
            catch { version = ""; }
            try { model = d["model"]; }
            catch { model = ""; }
            try { os = d["os"]; }
            catch { os = ""; }
            try { os_name = d["os_name"]; }
            catch { os_name = ""; }
            try { screen_size = d["screen_size"]; }
            catch { screen_size = ""; }
            try { type = d["type"]; }
            catch { type = ""; }
            try { page = d["page"]; }
            catch { page = "0"; }
            try { id = d["id"]; }
            catch { id = "1"; }
            try { session = d["session"]; }
            catch { session = ""; }
            try { uid = d["uid"]; }
            catch { uid = "0"; }
            try { chapterid = d["chapterid"]; }
            catch { chapterid = "0"; }
            try { bookid = d["bookid"]; }
            catch { bookid = "0"; }
            try { name = d["name"]; }
            catch { name = ""; }
            try { passwd = d["passwd"]; }
            catch { passwd = ""; }
            try { payment = d["payment"]; }
            catch { payment = "0.0"; }
            try { email = d["email"]; }
            catch { email = ""; }
            try { phone = d["phone"]; }
            catch { phone = ""; }
            try { cu = d["cu"]; }
            catch { cu = ""; }
            try { agent = d["agent"]; }
            catch { agent = ""; } 
            try { vid = d["vid"]; }
            catch { vid = ""; } 
            try { pid = d["pid"]; }
            catch { pid = ""; }
            try { firm = d["firm"]; }
            catch { firm = ""; }
            try { kernel = d["kernel"]; }
            catch { kernel = ""; }
            try { sgin = d["sgin"]; }
            catch { sgin = ""; }           
            try { ispay = d["ispay"]; }
            catch { ispay = ""; }

            switch (action.ToLower())
            {
                case "update":///更新
                    JsonStr = getUpdate(type,version);
                    break;
                case "message"://获取消息
                    JsonStr = getMessage(type,id);
                    break;
                case "install"://安装量
                    JsonStr = addInstall(type, imei, version, model, os, os_name, screen_size, cu, agent, vid, pid, firm, kernel);
                    break;
                case "getrecommend":
                    JsonStr = GetRecommend(page);
                    break;
                case "getsortlist":
                    JsonStr = GetSortList(id, page);
                    break;
                case "getbookinfo":
                    JsonStr = GetBookInfo(id);
                    break;
                case "getchapterlist":
                    JsonStr = GetChapterInfo(id, page,name);
                    break;
                case "getchaptercount":
                    JsonStr = GetChapterCount(id);
                    break;
                case "getcustomchapter":
                    JsonStr = GetCustomChapter(bookid, id, page);
                    break;
                case "downloads":
                    JsonStr = SetDownloads(id);
                    break;
                //case "hits":
                //    JsonStr = SetHits(id);
                //    break;
                case "sorttopic":
                    JsonStr = GetSortTopic(id);
                    break;
                case "search":
                    JsonStr = GetSearch(id, page);//id=keyword
                    break;
                case "tag":
                    JsonStr = GetTag();
                    break;
                case "chapter":
                    JsonStr = GetChapterPrice(bookid,chapterid,uid,ispay,session);
                    break;
                case "getuser":
                    JsonStr = GetUser(id, session);
                    break;
                case "pay":
                    JsonStr = Pay(uid, session, payment, bookid, chapterid);
                    break;
                case "isorders":
                    JsonStr = isOrders(uid, bookid, chapterid);
                    break;
                case "login":
                    JsonStr = Login(name, passwd);
                    break;
                case "ilogin":
                    JsonStr = Login(imei);
                    break;


                case "register":
                    JsonStr = Register(name, passwd, email, phone, imei);
                    break;
                case "about":
                    JsonStr = About();
                    break;
                case "topic":
                    JsonStr = GetTopic();
                    break;
                case "topiclist":
                    JsonStr=GetTopList(id,page);
                    break;
                case "bookprice":
                    JsonStr = GetBookPrice(bookid,chapterid);
                    break;
                case "recharge":
                    JsonStr = Recharge(uid, session, payment, type);
                    break;
                case "plug_recharge":
                    JsonStr = PlugRecharge(imei, payment, type, phone);
                    break;
                case "check":
                    JsonStr = CheckIn(uid, payment);
                    break;
                case "pushbookid":
                    JsonStr = GetPushBookId();
                    break;
                case "getbooklist":
                    JsonStr = GetBookList(id);
                    break;
                case "uplocation":
                    JsonStr = UpLocation(uid, imei, email, phone);
                    break;
                case "getnewcover":
                    JsonStr = GetNewCover();
                    break;



                default:
                    JsonStr = "{\"success\": false, \"result\": {}}";
                    break;
            }
            return JsonStr;

        }

        private static string GetNewCover()
        {
            StringBuilder sb = new StringBuilder();
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
            string newCoverId = appSettings.Settings["newCoverId"].Value;
            string newCover = appSettings.Settings["newCover"].Value;
            if (!string.IsNullOrEmpty(newCover))
            {
                sb.Append("{");
                sb.Append("\"result\":").Append("true").Append(",");
                sb.Append("\"url\":\"").Append(newCover).Append("\",");
                sb.Append("\"ID\":\"").Append(newCoverId).Append("\"");
                sb.Append("}");
            }
            else
            {
                sb.Append("{");
                sb.Append("\"result\":").Append("false").Append(",");
                sb.Append("\"url\":\"").Append(newCover).Append("\",");
                sb.Append("\"ID\":\"").Append(newCoverId).Append("\"");
                sb.Append("}");
            }
            return sb.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="imei"></param>
        /// <param name="Longitude">email经度</param>
        /// <param name="Latitude">phone纬度</param>
        /// <returns></returns>
        private static string UpLocation(string uid, string imei, string Longitude, string Latitude)
        {
            int UID = 0;
            if (!string.IsNullOrEmpty(uid)) { UID = int.Parse(uid); }
            string result = "false";
            int type = 0;
            if (!string.IsNullOrEmpty(imei) && !string.IsNullOrEmpty(Longitude) && !string.IsNullOrEmpty(Latitude))
            {


                string[] address = AdinZ.Common.GPS.GetAddress(double.Parse(Latitude), double.Parse(Longitude));
                Novel.Sys.Model.novel_location model = new Novel.Sys.Model.novel_location();
                model.Latitude = double.Parse(Latitude);
                model.Longitude = double.Parse(Longitude);
                model.IMEI = imei;
                if (address != null)
                {
                    model.Address = address[0];
                    if (address.Length > 2) { model.Province = address[1]; } else { model.Province = ""; }
                    if (address.Length > 3) { model.City = address[2]; } else { model.City = ""; }
                    if (address.Length >= 4) { model.District = address[3]; } else { model.District = ""; }
                }
                if (UID != 0)
                {
                    model.UID = UID;
                }
                model.Update = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                DB.LocalAdd(model);
                result = "true";
                type = 1;

            }

            return "{\"result\":" + result + ",\"type\":" + type + ",\"update\":\"" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\"}";
        }

        private static string GetPushBookId()
        {
            string result = "success", msg = "";
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            sb.Append("\"data\":[");
            DataSet ds = DB.GetPushBookId();
            int count = ds.Tables[0].Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    string id = ds.Tables[0].Rows[i]["id"].ToString(); if (string.IsNullOrEmpty(id)) id = "0";
                    string aid = ds.Tables[0].Rows[i]["aid"].ToString(); if (string.IsNullOrEmpty(aid)) aid = "0";
                    string type = ds.Tables[0].Rows[i]["type"].ToString(); if (string.IsNullOrEmpty(type)) type = "0";
                    string uid = ds.Tables[0].Rows[i]["uid"].ToString(); if (string.IsNullOrEmpty(uid)) uid = "0";
                    string addtime = ds.Tables[0].Rows[i]["addtime"].ToString(); if (string.IsNullOrEmpty(addtime)) addtime = "0";
                    if (string.IsNullOrEmpty(id)) id = "0";
                    sb.Append("{");
                    sb.Append("\"id\":").Append(id + ",");
                    sb.Append("\"aid\":").Append(aid + ",");
                    sb.Append("\"type\":").Append(type + ",");
                    sb.Append("\"uid\":").Append(uid + ",");
                    sb.Append("\"addtime\":").Append("\""+addtime+"\"");

                    if (i == ds.Tables[0].Rows.Count - 1) { sb.Append("}"); }
                    else
                    { sb.Append("},"); }
                }
            }
            else { result = "failure"; msg = "无数据"; }
            sb.Append("],");
            sb.Append("\"result\":\"").Append(result);
            sb.Append("\",\"count\":").Append(count);
            sb.Append(",\"msg\":\"").Append(msg);
            sb.Append("\"}");
            return Unicode.gbToUnicode(sb.ToString()); 
        }

        private static string GetBookList(string id)
        {
            string result = "success", msg = "";
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            sb.Append("\"data\":[");
            DataSet ds = DB.GetBookList(id);
            int count = ds.Tables[0].Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    sb.Append("{");
                    sb.Append("\"bookId\":").Append(ds.Tables[0].Rows[i]["id"].ToString() + ",");
                    sb.Append("\"bookName\":").Append("\"" + ds.Tables[0].Rows[i]["title"].ToString() + "\",");
                    sb.Append("\"bookAuthor\":").Append("\"" + ds.Tables[0].Rows[i]["author"].ToString() + "\",");
                    sb.Append("\"bookCaver\":").Append("\"" + ds.Tables[0].Rows[i]["cover"].ToString() + "\",");
                    sb.Append("\"bookIntor\":").Append("\"" + AdinZ.Common.Common.FilterHTML(ds.Tables[0].Rows[i]["intor"].ToString()) + "\",");
                    sb.Append("\"bookSortName\":").Append("\"" +DB.GetSortName(int.Parse(ds.Tables[0].Rows[i]["tid"].ToString())) + "\",");
                    sb.Append("\"bookSortId\":").Append(ds.Tables[0].Rows[i]["tid"].ToString() + ",");
                    sb.Append("\"bookSize\":").Append(ds.Tables[0].Rows[i]["size"].ToString() + ",");
                    sb.Append("\"bookHits\":").Append(ds.Tables[0].Rows[i]["hits"].ToString() + ",");
                    sb.Append("\"bookCreated\":").Append("\"" + ds.Tables[0].Rows[i]["created"].ToString() + "\",");
                    sb.Append("\"bookPermission\":").Append(ds.Tables[0].Rows[i]["permission"].ToString() + ",");
                    sb.Append("\"bookIsFull\":").Append(ds.Tables[0].Rows[i]["isfull"].ToString() + ",");
                    sb.Append("\"bookDownloads\":").Append(ds.Tables[0].Rows[i]["download"].ToString() == "" ? "0" : ds.Tables[0].Rows[i]["download"].ToString() ).Append(",");
                    sb.Append("\"bookPrice\":").Append(ds.Tables[0].Rows[i]["price"].ToString() + "");

                    if (i == ds.Tables[0].Rows.Count - 1) { sb.Append("}"); }
                    else
                    { sb.Append("},"); }
                }
            }
            else { result = "failure"; msg = "无数据"; }
            sb.Append("],");
            sb.Append("\"result\":\"").Append(result);
            sb.Append("\",\"count\":").Append(count);
            sb.Append(",\"msg\":\"").Append(msg);
            sb.Append("\"}");
            return Unicode.gbToUnicode(sb.ToString()); 
        }


        private static string CheckIn(string uid,string money)
        {
            StringBuilder sb = new StringBuilder();
            bool success = false;
            sb.Append("{");
            if (!string.IsNullOrEmpty(uid)&&!string.IsNullOrEmpty(money))
            {
                success = DB.CheckIn(int.Parse(uid), float.Parse(money));
                sb.Append("\"count\":").Append(DB.GetCheckCount(int.Parse(uid))).Append(",");
            } 
            sb.Append("\"result\":").Append(success.ToString().ToLower()).Append(",");
            sb.Append("\"msg\":\"").Append(success.ToString().ToLower()).Append("\"");
            sb.Append("}");
            return sb.ToString();
        }

        private static string GetBookPrice(string bookid,string chapterid)
        {
            StringBuilder sb = new StringBuilder();
            float price = 0.0f;
            sb.Append("{");
            if (!string.IsNullOrEmpty(bookid)&&!string.IsNullOrEmpty(chapterid))
            {
                if (DB.GetChapterNum(long.Parse(chapterid)) > 20) { 
                price = DB.GetBookPrice(int.Parse(bookid),long.Parse(chapterid));
                }
                else
                {
                    price = 0;
                }
            }
            else
            {
                price = 100f;
            }
            sb.Append("\"price\":").Append(price).Append("");
            sb.Append("}");
            return sb.ToString();
        }

        private static string GetTopic()
        {
            string resJson = @"{""data"":{},""result"":""success"",""count"":0,""msg"":""无数据""}";
            string result = "success", msg = "";

            int count = 0;
             DataSet ds = DB.GetTopic();
                StringBuilder sb = new StringBuilder();
                sb.Append("{");
                sb.Append("\"data\":[");
                count = ds.Tables[0].Rows.Count;
                if (count > 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append("{");
                        sb.Append("\"id\":").Append(ds.Tables[0].Rows[i]["id"].ToString() + ",");
                        sb.Append("\"type\":").Append(ds.Tables[0].Rows[i]["type"].ToString() + ",");
                        sb.Append("\"title\":\"").Append(ds.Tables[0].Rows[i]["title"].ToString() + "\",");
                        sb.Append("\"intor\":\"").Append(ds.Tables[0].Rows[i]["intor"].ToString() + "\",");
                        sb.Append("\"cover\":\"").Append("http://file.xiaoshuofang.com/").Append(ds.Tables[0].Rows[i]["cover"].ToString() + "\",");
                        sb.Append("\"created\":\"").Append(ds.Tables[0].Rows[i]["created"].ToString() + "\"");
                        if (i == ds.Tables[0].Rows.Count - 1) { sb.Append("}"); }
                        else
                        { sb.Append("},"); }
                    }
                }
                else { result = "failure"; }
                sb.Append("],");
                sb.Append("\"result\":\"");
                sb.Append(result);
                sb.Append("\",\"count\":");
                sb.Append(count);
                sb.Append(",\"msg\":\"");
                sb.Append(msg);
                sb.Append("\"}");
                resJson = Unicode.gbToUnicode(sb.ToString());
                return resJson;
        }

        private static string Register(string name, string passwd, string email, string phone, string imei)
        {
            if (!string.IsNullOrEmpty(name.Trim())&&!string.IsNullOrEmpty(passwd.Trim()))
            {
                if (email == null) email = ""; if (phone == null) { phone = ""; } if (imei == null) imei = "";
                return DB.Register(name.Trim(), passwd.Trim(), email.Trim(), phone.Trim(), imei.Trim()).ToString();
            }
            else { return 12 + ""; }
        }
        private static string GetSortTopic(string id)
        {
            string resJson = @"{""data"":{},""result"":""success"",""count"":0,""msg"":""无数据""}";
            string result = "success", msg = "";
            int count = 0;
            if (!string.IsNullOrEmpty(id))
            {
                DataSet ds = DB.GetSortTopic(int.Parse(id));
                StringBuilder sb = new StringBuilder();
                sb.Append("{");
                sb.Append("\"data\":[");
                count = ds.Tables[0].Rows.Count;
                if (count > 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append("{");
                        sb.Append("\"id\":").Append(ds.Tables[0].Rows[i]["id"].ToString() + ",");
                        sb.Append("\"name\":").Append("\"" + Unicode.gbToUnicode(ds.Tables[0].Rows[i]["title"].ToString()) + "\"");
                        if (i == ds.Tables[0].Rows.Count - 1) { sb.Append("}"); }
                        else
                        { sb.Append("},"); }
                    }
                }
                else { result = "failure"; }
                sb.Append("],");
                sb.Append("\"result\":\"");
                sb.Append(result);
                sb.Append("\",\"count\":");
                sb.Append(count);
                sb.Append(",\"msg\":\"");
                sb.Append(msg);
                sb.Append("\"}");
                resJson = sb.ToString();
            }
            return resJson;
        }


        private static string SetDownloads(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                return DB.SetDownloads(int.Parse(id)) + "";
            }
            else
            {
                return 0 + "";
            }
        }

        private static string GetChapterInfo(string id, string page,string model)
        {

            string resJson = @"{""data"":{},""result"":""success"",""count"":0,""msg"":""无数据""}";
            if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(page))
            {
                int pageSize = 50;
                string OrderBy = "";
                if (!string.IsNullOrEmpty(model))
                {
                    if (model.Equals(id))
                    {
                        pageSize = 20;
                        OrderBy = "chapternum DESC";
                    }
                }
                DataSet ds = DB.GetChapterList(int.Parse(id), int.Parse(page), pageSize, OrderBy);
                string result = "success", msg = "";

                StringBuilder sb = new StringBuilder();

                sb.Append("{");
                sb.Append("\"data\":[");

                int count = ds.Tables[0].Rows.Count;
                if (count > 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append("{");
                        sb.Append("\"chapterId\":").Append(ds.Tables[0].Rows[i]["id"].ToString() + ",");
                        sb.Append("\"chapterName\":").Append("\"" + Unicode.gbToUnicode(ds.Tables[0].Rows[i]["chapterName"].ToString()) + "\",");
                        sb.Append("\"size\":").Append(ds.Tables[0].Rows[i]["size"].ToString() + ",");

                        sb.Append("\"created\":").Append("\"" + ds.Tables[0].Rows[i]["created"].ToString() + "\",");
                        sb.Append("\"modified\":").Append("\"" + ds.Tables[0].Rows[i]["modified"].ToString() + "\",");
                        sb.Append("\"isVip\":").Append(ds.Tables[0].Rows[i]["isVip"].ToString() + ",");
                        sb.Append("\"price\":").Append(ds.Tables[0].Rows[i]["price"].ToString() + ",");
                        sb.Append("\"articlepath\":").Append("\"null\"");

                        if (i == ds.Tables[0].Rows.Count - 1) { sb.Append("}"); }
                        else
                        { sb.Append("},"); }

                    }

                }
                sb.Append("],");
                sb.Append("\"result\":\"");
                sb.Append(result);
                sb.Append("\",\"count\":").Append(0);
                sb.Append(",\"total\":").Append(DB.GetChapterCount(int.Parse(id)));
                sb.Append(",\"msg\":\"").Append(msg).Append("\"}");
                resJson = sb.ToString();
            }
            return resJson;
        }

        private static string GetChapterCount(string id)
        {
            string resJson = @"{""data"":{},""result"":""success"",""count"":0,""total"":0,""msg"":""无数据""}";
            if (!string.IsNullOrEmpty(id))
            {
                StringBuilder sb = new StringBuilder();
                string result = "success", msg = "";
                sb.Append("{");
                sb.Append("\"data\":[");
                sb.Append("],");
                sb.Append("\"result\":\"");
                sb.Append(result);
                sb.Append("\",\"count\":").Append(0);
                sb.Append(",\"total\":").Append(DB.GetChapterCount(int.Parse(id)));
                sb.Append(",\"msg\":\"").Append(msg).Append("\"}");
                resJson = sb.ToString();
            }
            return resJson;
        }


        private static string GetCustomChapter(string bookid, string id, string page)
        {   //bookid,id=开始的chapternum,page=结束的chapternum
            string resJson = @"{""data"":{},""result"":""success"",""count"":0,""msg"":""无数据""}";
            if (!string.IsNullOrEmpty(bookid) && !string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(page))
            {

                DataSet ds = DB.GetChapter(int.Parse(bookid), int.Parse(id), int.Parse(page));
                string result = "success", msg = "";

                StringBuilder sb = new StringBuilder();

                sb.Append("{");
                sb.Append("\"data\":[");

                int count = ds.Tables[0].Rows.Count;
                if (count > 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append("{");
                        sb.Append("\"chapterId\":").Append(ds.Tables[0].Rows[i]["id"].ToString() + ",");
                        sb.Append("\"chapterName\":").Append("\"" + Unicode.gbToUnicode(ds.Tables[0].Rows[i]["chapterName"].ToString()) + "\",");
                        sb.Append("\"size\":").Append(ds.Tables[0].Rows[i]["size"].ToString() + ",");

                        sb.Append("\"created\":").Append("\"" + ds.Tables[0].Rows[i]["created"].ToString() + "\",");
                        sb.Append("\"modified\":").Append("\"" + ds.Tables[0].Rows[i]["modified"].ToString() + "\",");
                        sb.Append("\"isVip\":").Append(ds.Tables[0].Rows[i]["isVip"].ToString() + ",");
                        sb.Append("\"price\":").Append(ds.Tables[0].Rows[i]["price"].ToString() + ",");
                        sb.Append("\"articlepath\":").Append("\"" + ds.Tables[0].Rows[i]["articlepath"].ToString() + "\"");

                        if (i == ds.Tables[0].Rows.Count - 1) { sb.Append("}"); }
                        else
                        { sb.Append("},"); }

                    }

                }
                sb.Append("],");
                sb.Append("\"result\":\"");
                sb.Append(result);
                sb.Append("\",\"count\":").Append(0);
                sb.Append(",\"total\":").Append(DB.GetChapterCount(int.Parse(bookid)));
                sb.Append(",\"msg\":\"").Append(msg).Append("\"}");
                resJson = sb.ToString();
            }
            return resJson;
        }


        private static string GetBookInfo(string id)
        {
            Novel.Sys.Model.novel_article model = new Novel.Sys.Model.novel_article();
            string resJson = @"{""data"":{},""result"":""success"",""count"":0,""msg"":""无数据""}";
            if (!string.IsNullOrEmpty(id))
            {
                DB.SetHits(int.Parse(id));
                string result = "success", msg = "";
                model = DB.GetBookInfo(int.Parse(id));

                StringBuilder sb = new StringBuilder();
                sb.Append("{");
                sb.Append("\"data\":{");
                if (model == null) { result = "failure"; msg = "无此小说"; }
                else
                {
                    sb.Append("\"bookId\":").Append(model.ID + ",");
                    sb.Append("\"bookName\":").Append("\"" + AdinZ.Common.Unicode.gbToUnicode(model.Title) + "\",");
                    sb.Append("\"bookAuthor\":").Append("\"" + AdinZ.Common.Unicode.gbToUnicode(model.Author) + "\",");
                    sb.Append("\"bookCaver\":").Append("\"" + model.Cover + "\",");
                    sb.Append("\"bookIntor\":").Append("\"" + AdinZ.Common.Unicode.gbToUnicode(AdinZ.Common.Common.FilterHTML(model.Intor)) + "\",");
                    sb.Append("\"bookSortName\":").Append("\"" + AdinZ.Common.Unicode.gbToUnicode(DB.GetSortName(model.Tid)) + "\",");
                    sb.Append("\"bookSortId\":").Append(model.Tid + ",");
                    sb.Append("\"bookSize\":").Append(model.Size + ",");
                    sb.Append("\"bookHits\":").Append(model.Hits + ",");
                    sb.Append("\"bookCreated\":").Append("\"" + model.Created + "\",");
                    sb.Append("\"bookPermission\":").Append(model.Permission + ",");
                    sb.Append("\"bookIsFull\":").Append(model.Isfull + ",");
                    sb.Append("\"bookDownloads\":").Append(model.Downloads + ",");
                    sb.Append("\"bookPrice\":").Append(model.Price + ",");
                    sb.Append("\"AdStr\":").Append("\"" + AdinZ.Common.Unicode.gbToUnicode(DB.GetRandomAds()) + "\"");
                }



                sb.Append("},");
                sb.Append("\"result\":\"");
                sb.Append(result);
                sb.Append("\",\"count\":");
                sb.Append(0);
                sb.Append(",\"msg\":\"");
                sb.Append(msg);
                sb.Append("\"}");
                resJson = sb.ToString();
            }
            return resJson;
        }

        private static string getUpdate(string type,string Version)
        {
            string JsonStr = "{\"success\": false, \"result\": {}}";
            DataSet ds = DB.getUpdate(type);
            try
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    float version = float.Parse(ds.Tables[0].Rows[0]["version"].ToString());
                    string title = ds.Tables[0].Rows[0]["title"].ToString();
                    string body = ds.Tables[0].Rows[0]["body"].ToString();
                    string url = ds.Tables[0].Rows[0]["url"].ToString();
                    string update = ds.Tables[0].Rows[0]["update"].ToString();
                    if (version > float.Parse(Version))
                    {
                        JsonStr = "{\"success\": true, \"result\": {\"title\":\""
                        + AdinZ.Common.Unicode.gbToUnicode(title) + "\",\"body\":\""
                        + AdinZ.Common.Unicode.gbToUnicode(body) + "\",\"url\":\"" + url
                        + "\",\"version\":" + version + ",\"update\":\""
                        + update + "\"}}";
                    }
                }
            }
            catch { JsonStr = "{\"success\": false, \"result\": {}}"; }
            return JsonStr;
        }


        private static string getMessage(string type,string id)
        {
            StringBuilder JSONSrc = new StringBuilder();

            string JsonStr = "{\"success\": false, \"result\": {}}";
            DataSet ds =null;
            if (string.IsNullOrEmpty(id))
            {
                ds = DB.getMessage(type);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    string title = ds.Tables[0].Rows[0]["title"].ToString();
                    string body = ds.Tables[0].Rows[0]["body"].ToString();
                    string enddate = ds.Tables[0].Rows[0]["enddate"].ToString();
                    string update = ds.Tables[0].Rows[0]["update"].ToString();
                    string imei = ds.Tables[0].Rows[0]["imei"].ToString();
                    DateTime dt = DateTime.Now;
                    if (dt < DateTime.Parse(enddate))
                    {
                        JsonStr = "{\"success\": true, \"result\": {\"title\":\""
                                + AdinZ.Common.Unicode.gbToUnicode(title) + "\",\"body\":\""
                                + AdinZ.Common.Unicode.gbToUnicode(body) + "\",\"enddate\":\""
                                + DateTime.Parse(enddate).ToString("yyyy-MM-dd hh:mm:ss")+ "\",\"update\":\"" 
                                + DateTime.Parse(update).ToString("yyyy-MM-dd hh:mm:ss") + "\",\"imei\":\"" + imei + "\"}}";
                    }
                }
            }
            else
            {
                ds = DB.getMessage(type, int.Parse(id));
                if (ds.Tables[0].Rows.Count > 0)
                {
                    string title = ds.Tables[0].Rows[0]["title"].ToString();
                    string body = ds.Tables[0].Rows[0]["body"].ToString();
                    string enddate = ds.Tables[0].Rows[0]["enddate"].ToString();
                    string update = ds.Tables[0].Rows[0]["update"].ToString();
                    string imei = ds.Tables[0].Rows[0]["imei"].ToString();
                    JSONSrc.Append("{")
                        .Append("\"success\":").Append("true,")
                            .Append("\"result\": {")
                                .Append("\"title\":\"").Append(AdinZ.Common.Unicode.gbToUnicode(title)).Append("\",")
                                .Append("\"body\":\"").Append(AdinZ.Common.Unicode.gbToUnicode(body)).Append("\",")
                                .Append("\"enddate\":\"").Append(DateTime.Parse(enddate).ToString("yyyy-MM-dd hh:mm:ss")).Append("\",")
                                .Append("\"update\":\"").Append(DateTime.Parse(update).ToString("yyyy-MM-dd hh:mm:ss")).Append("\",")
                                .Append("\"imei\":\"").Append(imei).Append("\"")
                            .Append("}")
                        .Append("}");
                    JsonStr = JSONSrc.ToString();
                }
            }
            return JsonStr;
        }

        private static string addInstall(string type, string imei, string version, string model, string os, string os_name, string screen_size, string cu, string agent, string vid, string pid, string firm, string kernel)
        {
            string JsonStr = "{\"success\": false, \"result\": 0}";
            int i = 0;
            if (imei.Length < 3)
            {
                return JsonStr;
            }
            else
            {
                if (!DB.isExistInstall(imei, type))
                {

                    i = DB.addInstall(type, imei, version, model, os, os_name, screen_size, cu, agent, vid, pid, firm, kernel);
                    JsonStr = "{\"success\": true, \"result\": " + i + "}";
                }
                else
                    JsonStr = "{\"success\": true, \"result\": 1}";
                return JsonStr;
            }
        }

        private static string GetChapterPrice(string bookid,string chapterid,string uid,string ispay,string session)
        {
            Novel.Sys.Model.novel_chapter chapter = new Novel.Sys.Model.novel_chapter();
            Novel.Sys.Model.novel_article book = new Novel.Sys.Model.novel_article();
            Novel.Sys.Model.novel_user_info user = new Novel.Sys.Model.novel_user_info();
            StringBuilder sb = new StringBuilder();
            string msg = "";
            int msgno = 0;
            string path = "";
            int size = 0;
            int UID = 0;
            sb.Append("{");
            float price = 0f;
            bool isPay = false; 
            bool vip = false;
            if (!string.IsNullOrEmpty(ispay))
            {
                try { isPay = bool.Parse(ispay); }
                catch { isPay = false; }
            }
            try
            {
                if (!string.IsNullOrEmpty(chapterid))
                {
                    chapter = DB.GetChapter(int.Parse(chapterid));
                    if (chapter.Chapternum > 20)
                    {
                        book = DB.GetBookInfo(int.Parse(bookid));
                        if (book != null)
                        {
                            price = book.Price;
                            if (price > 0)
                            {
                                if (!string.IsNullOrEmpty(uid))
                                {
                                    UID = int.Parse(uid);
                                    if (UID > 0) { user = DB.GetUserInfo(UID); }
                                    else { user = null; }
                                    if (user != null)
                                    {
                                        if (DB.isOrders(UID, int.Parse(bookid)))
                                        {
                                            msg = "已经订阅";
                                            msgno = 0;
                                            path = "http://file.xiaoshuofang.com/" + chapter.Articlepath;
                                            size = chapter.Size;
                                        }
                                        else
                                        {//没有订阅过
                                            if (user.Money > price)
                                            {
                                                if (isPay)
                                                {
                                                    if (DB.Pay(UID, book.Price, int.Parse(bookid), long.Parse(chapterid)))
                                                    {
                                                        msg = "订阅成功";
                                                        msgno = 0;
                                                        path = "http://file.xiaoshuofang.com/" + chapter.Articlepath;
                                                        size = chapter.Size;
                                                    }
                                                    else
                                                    {
                                                        msg = "订购失败";
                                                        msgno = 6;
                                                    }
                                                }
                                                else
                                                {
                                                    msg = "是否订阅本书";
                                                    msgno = 5;
                                                }
                                            }
                                            else
                                            {
                                                msg = "金币不足";
                                                msgno = 4;
                                            }
                                        }//没有订阅过
                                    }
                                    else
                                    {
                                        msg = "无此会员";
                                        msgno = 3;
                                    }
                                }
                                else
                                {
                                    msg = "请登陆";
                                    msgno = 1;
                                }
                            }
                            else//免费小说
                            {
                                msg = "";
                                msgno = 0;
                                path = "http://file.xiaoshuofang.com/" + chapter.Articlepath;
                                size = chapter.Size;
                            }
                        }
                        else
                        {
                            msg = "获取书籍错误";
                            msgno = 2;
                        }
                    }
                    else//前20章
                    {
                        msg = "";
                        msgno = 0;
                        path = "http://file.xiaoshuofang.com/" + chapter.Articlepath;
                        size = chapter.Size;
                    }
                }
                else
                {
                    msg = "Chapters id can not be empty";
                    msgno = 10;
                }
               
                if (chapter.Isvip == 1) vip = true;
            }catch{
                msg = "未知错误";
                msgno = 100;
            }
            sb.Append("\"vip\":").Append(vip.ToString().ToLower()).Append(",");
            sb.Append("\"price\":").Append(price).Append(",");
            sb.Append("\"path\":\"").Append(path).Append("\",");
            sb.Append("\"size\":").Append(size).Append(",");
            sb.Append("\"msgno\":").Append(msgno).Append(",");
            sb.Append("\"msg\":\"").Append(msg).Append("\"");
            sb.Append("}");
            return Unicode.gbToUnicode(sb.ToString());
        }


        /// <summary>
        /// 获取特荐数据
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        private static string GetRecommend(string page)
        {
            string resJson = @"{""data"":{},""result"":""success"",""code"":0,""msg"":""无数据""}";
            string result = "success", msg = "";
            int count = 0, downloads = 0;
            DataSet ds = DB.GetRecommend(int.Parse(page), 20);
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            sb.Append("\"data\":[");

            count = ds.Tables[0].Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["download"].ToString()))
                    {
                        downloads = int.Parse(ds.Tables[0].Rows[i]["download"].ToString());
                    }
                    else { downloads = 0; }
                    sb.Append("{");
                    sb.Append("\"id\":").Append(ds.Tables[0].Rows[i]["id"].ToString() + ",");
                    sb.Append("\"name\":").Append("\"" + Unicode.gbToUnicode(ds.Tables[0].Rows[i]["title"].ToString()) + "\",");
                    sb.Append("\"intor\":").Append("\"" + Unicode.gbToUnicode(AdinZ.Common.StringPlus.Left(AdinZ.Common.Common.FilterHTML(ds.Tables[0].Rows[i]["intor"].ToString()), 60)) + "\",");
                    sb.Append("\"cover\":").Append("\"http://file.xiaoshuofang.com/" + ds.Tables[0].Rows[i]["cover"].ToString() + "\",");
                    sb.Append("\"author\":").Append("\"" + Unicode.gbToUnicode(ds.Tables[0].Rows[i]["author"].ToString()) + "\",");
                    sb.Append("\"size\":").Append(ds.Tables[0].Rows[i]["size"].ToString() + ",");
                    sb.Append("\"isFull\":").Append(ds.Tables[0].Rows[i]["isFull"].ToString() + ",");
                    sb.Append("\"downloads\":").Append(downloads + ",");
                    sb.Append("\"hits\":").Append(ds.Tables[0].Rows[i]["hits"].ToString());
                    if (i == ds.Tables[0].Rows.Count - 1) { sb.Append("}"); }
                    else
                    { sb.Append("},"); }

                }

            }
            else { result = "failure"; }




            sb.Append("],");
            sb.Append("\"result\":\"");
            sb.Append(result);
            sb.Append("\",\"count\":");
            sb.Append(count);
            sb.Append(",\"msg\":\"");
            sb.Append(msg);
            sb.Append("\"}");

            resJson = sb.ToString();
            return resJson;
        }

        /// <summary>
        /// 根据分类id获取数据
        /// </summary>
        /// <param name="tid"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        private static string GetSortList(string tid, string page)
        {
            string resJson = @"{""data"":{},""result"":""success"",""count"":0,""msg"":""无数据""}";
            string result = "success", msg = "";
            int count = 0, downloads = 0;
            DataSet ds = DB.GetSortList(int.Parse(tid), int.Parse(page), 20);
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            sb.Append("\"data\":[");
            count = ds.Tables[0].Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["download"].ToString()))
                    {
                        downloads = int.Parse(ds.Tables[0].Rows[i]["download"].ToString());
                    }
                    else { downloads = 0; }
                    sb.Append("{");
                    sb.Append("\"id\":").Append(ds.Tables[0].Rows[i]["id"].ToString() + ",");
                    sb.Append("\"name\":").Append("\"" + Unicode.gbToUnicode(ds.Tables[0].Rows[i]["title"].ToString()) + "\",");
                    sb.Append("\"intor\":").Append("\"" + Unicode.gbToUnicode(AdinZ.Common.StringPlus.Left(AdinZ.Common.Common.FilterHTML(ds.Tables[0].Rows[i]["intor"].ToString()), 60)) + "\",");
                    sb.Append("\"cover\":").Append("\"http://file.xiaoshuofang.com/" + ds.Tables[0].Rows[i]["cover"].ToString() + "\",");
                    sb.Append("\"author\":").Append("\"" + Unicode.gbToUnicode(ds.Tables[0].Rows[i]["author"].ToString()) + "\",");
                    sb.Append("\"size\":").Append(ds.Tables[0].Rows[i]["size"].ToString() + ",");
                    sb.Append("\"isFull\":").Append(ds.Tables[0].Rows[i]["isFull"].ToString() + ",");
                    sb.Append("\"downloads\":").Append(downloads + ",");
                    sb.Append("\"hits\":").Append(ds.Tables[0].Rows[i]["hits"].ToString());

                    if (i == ds.Tables[0].Rows.Count - 1) { sb.Append("}"); }
                    else
                    { sb.Append("},"); }
                }
            }
            else { result = "failure"; }
            sb.Append("],");
            sb.Append("\"result\":\"");
            sb.Append(result);
            sb.Append("\",\"count\":");
            sb.Append(count);
            sb.Append(",\"msg\":\"");
            sb.Append(msg);
            sb.Append("\"}");

            resJson = sb.ToString();
            return resJson;

        }



        private static string GetTopList(string id, string page)
        {
            StringBuilder sb = new StringBuilder();
            string resJson = @"{""data"":{},""result"":""success"",""count"":0,""msg"":""无数据""}";
            string result = "success", msg = "";
            int count = 0, downloads = 0;
            if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(page))
            {
                DataSet ds = DB.GeTopicList(int.Parse(id), int.Parse(page), 20);
                sb.Append("{");
                sb.Append("\"data\":[");
                count = ds.Tables[0].Rows.Count;
                if (count > 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["download"].ToString()))
                        {
                            downloads = int.Parse(ds.Tables[0].Rows[i]["download"].ToString());
                        }
                        else { downloads = 0; }
                        sb.Append("{");
                        sb.Append("\"id\":").Append(ds.Tables[0].Rows[i]["id"].ToString() + ",");
                        sb.Append("\"name\":").Append("\"" + Unicode.gbToUnicode(ds.Tables[0].Rows[i]["title"].ToString()) + "\",");
                        sb.Append("\"intor\":").Append("\"" + Unicode.gbToUnicode(AdinZ.Common.StringPlus.Left(AdinZ.Common.Common.FilterHTML(ds.Tables[0].Rows[i]["intor"].ToString()), 60)) + "\",");
                        sb.Append("\"cover\":").Append("\"http://file.xiaoshuofang.com/" + ds.Tables[0].Rows[i]["cover"].ToString() + "\",");
                        sb.Append("\"author\":").Append("\"" + Unicode.gbToUnicode(ds.Tables[0].Rows[i]["author"].ToString()) + "\",");
                        sb.Append("\"size\":").Append(ds.Tables[0].Rows[i]["size"].ToString() + ",");
                        sb.Append("\"isFull\":").Append(ds.Tables[0].Rows[i]["isFull"].ToString() + ",");
                        sb.Append("\"downloads\":").Append(downloads + ",");
                        sb.Append("\"hits\":").Append(ds.Tables[0].Rows[i]["hits"].ToString());

                        if (i == ds.Tables[0].Rows.Count - 1) { sb.Append("}"); }
                        else
                        { sb.Append("},"); }
                    }
                }
                else { result = "failure"; }
            }
            sb.Append("],");
            sb.Append("\"result\":\"");
            sb.Append(result);
            sb.Append("\",\"count\":");
            sb.Append(count);
            sb.Append(",\"msg\":\"");
            sb.Append(msg);
            sb.Append("\"}");

            resJson = sb.ToString();
            return resJson;
        }


        private static string GetSearch(string keyword, string page)
        {

            DB.addSearch(keyword);
            string resJson = @"{""data"":{},""result"":""success"",""count"":0,""msg"":""无数据""}";
            string result = "success", msg = "";
            int count = 0, downloads = 0;
            DataSet ds = DB.GetSearch(keyword, int.Parse(page), 20);
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            sb.Append("\"data\":[");
            count = ds.Tables[0].Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["download"].ToString()))
                    {
                        downloads = int.Parse(ds.Tables[0].Rows[i]["download"].ToString());
                    }
                    else { downloads = 0; }
                    sb.Append("{");
                    sb.Append("\"id\":").Append(ds.Tables[0].Rows[i]["id"].ToString() + ",");
                    sb.Append("\"name\":").Append("\"" + Unicode.gbToUnicode(ds.Tables[0].Rows[i]["title"].ToString()) + "\",");
                    sb.Append("\"intor\":").Append("\"" + Unicode.gbToUnicode(AdinZ.Common.StringPlus.Left(AdinZ.Common.Common.FilterHTML(ds.Tables[0].Rows[i]["intor"].ToString()), 60)) + "\",");
                    sb.Append("\"cover\":").Append("\"http://file.xiaoshuofang.com/" + ds.Tables[0].Rows[i]["cover"].ToString() + "\",");
                    sb.Append("\"author\":").Append("\"" + Unicode.gbToUnicode(ds.Tables[0].Rows[i]["author"].ToString()) + "\",");
                    sb.Append("\"size\":").Append(ds.Tables[0].Rows[i]["size"].ToString() + ",");
                    sb.Append("\"isFull\":").Append(ds.Tables[0].Rows[i]["isFull"].ToString() + ",");
                    sb.Append("\"downloads\":").Append(downloads + ",");
                    sb.Append("\"hits\":").Append(ds.Tables[0].Rows[i]["hits"].ToString());

                    if (i == ds.Tables[0].Rows.Count - 1) { sb.Append("}"); }
                    else
                    { sb.Append("},"); }
                }
            }
            else { result = "failure"; }
            sb.Append("],");
            sb.Append("\"result\":\"");
            sb.Append(result);
            sb.Append("\",\"count\":");
            sb.Append(count);
            sb.Append(",\"msg\":\"");
            sb.Append(msg);
            sb.Append("\"}");

            resJson = sb.ToString();
            return resJson;

        }

        private static string GetTag()
        {
            string resJson = @"{""data"":{},""result"":""success"",""count"":0,""msg"":""无数据""}";
            string result = "success", msg = "";
            int count = 0;
            DataSet ds = DB.GetTag(50);
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            sb.Append("\"data\":[");
            count = ds.Tables[0].Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    sb.Append("{");
                    sb.Append("\"keyword\":").Append("\"" + Unicode.gbToUnicode(ds.Tables[0].Rows[i]["keyword"].ToString()) + "\",");
                    sb.Append("\"count\":").Append(ds.Tables[0].Rows[i]["count"].ToString());
                    if (i == ds.Tables[0].Rows.Count - 1) { sb.Append("}"); }
                    else
                    { sb.Append("},"); }
                }
            }
            else { result = "failure"; }
            sb.Append("],");
            sb.Append("\"result\":\"");
            sb.Append(result);
            sb.Append("\",\"count\":");
            sb.Append(count);
            sb.Append(",\"msg\":\"");
            sb.Append(msg);
            sb.Append("\"}");

            resJson = sb.ToString();
            return resJson;
        }

        private static string GetUser(string id, string session)
        {
            bool result = false;
            string msg = "";
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(session))
            {
                Novel.Sys.Model.novel_user_info info = new Novel.Sys.Model.novel_user_info();
                info = DB.GetUser(int.Parse(id), session);
                if (info != null)
                {
                    if (info.Session.Endtime > DateTime.Now)
                    {
                        sb.Append("\"id\":").Append(info.User.ID).Append(",");
                        sb.Append("\"gid\":").Append(info.User.Gid).Append(",");
                        sb.Append("\"nick\":\"").Append(info.User.Nick+"\",");
                        sb.Append("\"name\":\"").Append(info.User.Name+"\",");
                        sb.Append("\"email\":\"").Append(info.User.Email+"\",");
                        sb.Append("\"phone\":\"").Append(info.User.Phone+"\",");
                        sb.Append("\"avatar\":\"").Append(info.User.Avatar +"\",");
                        sb.Append("\"sex\":").Append(info.User.Sex).Append(",");
                        sb.Append("\"session\":\"").Append(info.Session.Session).Append("\",");
                        sb.Append("\"endtime\":\"").Append(info.Session.Endtime).Append("\",");

                        sb.Append("\"imei\":\"").Append(info.Imei == null ? "" : info.Imei).Append("\",");
                        sb.Append("\"money\":").Append(info.Money).Append(",");
                        sb.Append("\"gold\":").Append(info.Gold).Append(",");
                        sb.Append("\"signature\":\"").Append(Unicode.gbToUnicode(info.Signature)).Append("\",");
                        result= true;
                    }
                    else { result = false; msg = "Session过期"; }
                }
                else { result = false; msg = "Session不存在"; }

            }
            else { result = false; msg = "缺少id,session"; }

            sb.Append("\"result\":").Append(result.ToString().ToLower() + ",");
            sb.Append("\"msg\":\"").Append(Unicode.gbToUnicode(msg.ToString()) + "\"");

            sb.Append("}");

            return sb.ToString();
        }
        private static string Login(string name, string passwd)
        {
            StringBuilder sb = new StringBuilder();
            bool result = false;
            string msg = "";
            sb.Append("{");
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(passwd))
            {
                Novel.Sys.Model.novel_user_info info = new Novel.Sys.Model.novel_user_info();
                info = DB.Login(name, passwd);
                if (info != null)
                {
                    sb.Append("\"id\":").Append(info.User.ID).Append(",");
                    sb.Append("\"gid\":").Append(info.User.Gid).Append(",");
                    sb.Append("\"nick\":\"").Append(info.User.Nick+"\",");
                    sb.Append("\"name\":\"").Append(info.User.Name +"\",");
                    sb.Append("\"email\":\"").Append(info.User.Email+"\",");
                    sb.Append("\"phone\":\"").Append(info.User.Phone +"\",");
                    sb.Append("\"avatar\":\"").Append("http://file.xiaoshuofang.com/"+info.User.Avatar +"\",");
                    sb.Append("\"sex\":").Append(info.User.Sex).Append(",");
                    sb.Append("\"session\":\"").Append(info.Session.Session).Append("\",");
                    sb.Append("\"endtime\":\"").Append(info.Session.Endtime).Append("\",");

                    sb.Append("\"imei\":\"").Append(info.Imei +"\",");
                    sb.Append("\"money\":").Append(info.Money).Append(",");
                    sb.Append("\"gold\":").Append(info.Gold).Append(",");
                    sb.Append("\"signature\":\"").Append(Unicode.gbToUnicode(info.Signature)).Append("\",");
                    result = true;
                }
            }
            else { result = false; msg = "请输入用户名或密码"; }

            sb.Append("\"result\":").Append(result.ToString().ToLower() + ",");
            sb.Append("\"msg\":\"").Append(Unicode.gbToUnicode(msg.ToString()) + "\"");

            sb.Append("}");
            return sb.ToString();
        }


        private static string Login(string imei)
        {
            StringBuilder sb = new StringBuilder();
            bool result = false;
            string msg = "";
            sb.Append("{");
            if (!string.IsNullOrEmpty(imei))
            {
                Novel.Sys.Model.novel_user_info info = new Novel.Sys.Model.novel_user_info();
                info = DB.Login(imei);
                if (info != null)
                {
                    sb.Append("\"id\":").Append(info.User.ID).Append(",");
                    sb.Append("\"gid\":").Append(info.User.Gid).Append(",");
                    sb.Append("\"nick\":\"").Append(info.User.Nick + "\",");
                    sb.Append("\"name\":\"").Append(info.User.Name + "\",");
                    sb.Append("\"email\":\"").Append(info.User.Email + "\",");
                    sb.Append("\"phone\":\"").Append(info.User.Phone + "\",");
                    sb.Append("\"avatar\":\"").Append("http://file.xiaoshuofang.com/" + info.User.Avatar + "\",");
                    sb.Append("\"sex\":").Append(info.User.Sex).Append(",");
                    sb.Append("\"session\":\"").Append(info.Session.Session).Append("\",");
                    sb.Append("\"endtime\":\"").Append(info.Session.Endtime).Append("\",");

                    sb.Append("\"imei\":\"").Append(info.Imei + "\",");
                    sb.Append("\"money\":").Append(info.Money).Append(",");
                    sb.Append("\"gold\":").Append(info.Gold).Append(",");
                    sb.Append("\"signature\":\"").Append(Unicode.gbToUnicode(info.Signature)).Append("\",");
                    result = true;
                }
            }
            else { result = false; msg = "请输入用户名或密码"; }

            sb.Append("\"result\":").Append(result.ToString().ToLower() + ",");
            sb.Append("\"msg\":\"").Append(Unicode.gbToUnicode(msg.ToString()) + "\"");

            sb.Append("}");
            return sb.ToString();
        }









        private static string Pay(string uid, string session, string payment, string bookid, string chapterid)
        {
            StringBuilder sb = new StringBuilder();
            bool success = false;
            sb.Append("{");
            if (!string.IsNullOrEmpty(uid) && !string.IsNullOrEmpty(session) && !string.IsNullOrEmpty(payment) && !string.IsNullOrEmpty(bookid) && !string.IsNullOrEmpty(chapterid))
            {
                try
                {
                    success = DB.Pay(int.Parse(uid), float.Parse(payment), int.Parse(bookid), long.Parse(chapterid));
                }
                catch { success = false; }
            }
            sb.Append("\"result\":").Append(success.ToString().ToLower()).Append(",");
            sb.Append("\"msg\":\"").Append(success.ToString().ToLower()).Append("\"");
            sb.Append("}");
            return sb.ToString();
        }

        public static string Recharge(string uid, string session, string payment, string type)
        {
            StringBuilder sb = new StringBuilder();
            bool success = false;
            sb.Append("{");
            if (!string.IsNullOrEmpty(uid) && !string.IsNullOrEmpty(session) && !string.IsNullOrEmpty(payment))
            {
                try
                {
                    success = DB.Recharge(int.Parse(uid), float.Parse(payment), type);
                }
                catch { success = false; }
            }
            sb.Append("\"result\":").Append(success.ToString().ToLower()).Append(",");
            sb.Append("\"msg\":\"").Append(success.ToString().ToLower()).Append("\"");
            sb.Append("}");
            return sb.ToString();
        }


        private static string PlugRecharge(string imei, string payment, string type, string intor)
        {
            StringBuilder sb = new StringBuilder();
            bool success = false;
            sb.Append("{");
            if (!string.IsNullOrEmpty(imei) && !string.IsNullOrEmpty(payment) )
            {
                try
                {
                    success = DB.PlugRecharge(imei, float.Parse(payment), type, intor);
                }
                catch { success = false; }
            }
            sb.Append("\"result\":").Append(success.ToString().ToLower()).Append(",");
            sb.Append("\"msg\":\"").Append(success.ToString().ToLower()).Append("\"");
            sb.Append("}");
            return sb.ToString();
        }

        private static string isOrders(string uid, string bookid, string chapterid)
        {
            StringBuilder sb = new StringBuilder();
            bool success = false;
            sb.Append("{");
            if (!string.IsNullOrEmpty(uid) && !string.IsNullOrEmpty(bookid))
            {
                try
                {
                    success = DB.isOrders(int.Parse(uid), int.Parse(bookid));
                }
                catch { success = false; }
            }
            sb.Append("\"result\":").Append(success.ToString().ToLower());
            sb.Append("}");
            return sb.ToString();
        }


        private static string About()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("</b><br/>此版本适用于Android2.0以上操作系统<br/>对于在使用其他操作系统的设备上使用本软件，出现的任何问题本软件作者不承担任何责任。<br/>更多免费小说请到【<a href=\"http://www.xiaoshuofang.com\">小说坊</a>】下载" +
                "<br/><a href=\"http://www.xiaoshuofang.com\">XiaoShuoFang.Com</a><br/>" +
                "交流QQ群:372607668<br/>" +
                "欢迎大家加入.交流及反馈,求书!");
            return sb.ToString();
        }







    }
}
